
capture drop gcic2
egen gcic2=group(cic2)
egen gmax=max(gcic2)
capture drop tfp


local rrm=gmax
display `rrm'
foreach cc of numlist 1/`rrm'{
preserve
  keep if gcic2==`cc'
replace x1=0 if x1==.
replace x2=0 if x2==.
replace x3=0 if x3==.

replace y1=0 if y1==.
replace y2=0 if y2==.
replace y3=0 if y3==.

***使用雇佣人数来衡量l，也可以使用工资来衡量l
***将tariff替换成了x，但是觉着tariff_lag1相当于X1

rename lny q
rename lnl l
rename lnm m
rename lnk k
sort firm year
duplicates drop firm year, force


*** edits begin here ***
gen exp_dummy99=0
gen exp_dummy00=0
gen exp_dummy01=0
gen exp_dummy02=0
gen exp_dummy03=0
gen exp_dummy04=0
gen exp_dummy05=0
replace exp_dummy99=exp_dummy if year==1999
replace exp_dummy00=exp_dummy if year==2000
replace exp_dummy01=exp_dummy if year==2001
replace exp_dummy02=exp_dummy if year==2002
replace exp_dummy03=exp_dummy if year==2003
replace exp_dummy04=exp_dummy if year==2004
replace exp_dummy05=exp_dummy if year==2005


for var q l k m exp_dummy99 exp_dummy00 exp_dummy01 exp_dummy02 exp_dummy03 exp_dummy04 exp_dummy05: bysort firm (year): gen X_lag = X[_n-1]
for var l k m l_lag k_lag m_lag exp_dummy99_lag exp_dummy00_lag exp_dummy01_lag exp_dummy02_lag exp_dummy03_lag exp_dummy04_lag exp_dummy05_lag: gen X2=X^2
for any m l m_lag l_lag: gen Xk=X*k
for any m l m_lag l_lag: gen Xk_lag=X*k_lag
for any m m_lag \ any l l_lag: gen XY=X*Y
gen mlk=l*m*k
gen m_lagl_lagk_lag=m_lag*l_lag*k_lag
gen m_lagl_lagk=m_lag*l_lag*k
 forvalues ll = 0/3 {
	local kmax = 3 - `ll'
	forvalues kk = 0/`kmax' {
		local mmax = 3 - `ll' - `kk'
		forvalues mm = 0/`mmax' {
			gen NON`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')
			gen EXP99`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')*exp_dummy99
			gen EXP00`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')*exp_dummy00
			gen EXP01`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')*exp_dummy01
			gen EXP02`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')*exp_dummy02
			gen EXP03`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')*exp_dummy03
			gen EXP04`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')*exp_dummy04
			gen EXP05`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')*exp_dummy05
			gen X1`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')*x1
			gen X2`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')*x2
			gen X3`ll'`kk'`mm'=(l^`ll')*(k^`kk')*(m^`mm')*x3

			
			
		}
	}
}	
///
gen missing=q+k+l+m+exp_dummy99+exp_dummy00+exp_dummy01+exp_dummy02+exp_dummy03+exp_dummy04+exp_dummy05
bysort firm (year): gen missing_lag=missing[_n-1]


*drop if missing==. | (missing_lag==. & year~=1998)


codebook year SDcic
*drop if SDcic~=0
drop missing missing_lag SDcic
destring firm,replace
local estsample:tempfile

     capture xi: areg q NON* EXP* X1* X2* X3* x1 x2 x3 i.year ,absorb(cic_adj)
	*capture xi: areg q NON* EXP*   i.year ,absorb(cic_adj)
	if _rc==2001 continue
	qui predict phi
	qui gsort firm + year
	
	egen firmid =group(firm)
    sort firmid year
	bysort firmid: gen phi_lag=L.phi
	qui drop _I*
	qui gen const=1
*	for var q l_lag k phi phi_lag: qui drop if X==.

		*OLS
	qui xi: regress q m l x1 x2 x3 k exp_dummy99 exp_dummy00 exp_dummy01 exp_dummy02 exp_dummy03 exp_dummy04 exp_dummy05 i.cic_adj i.year 
	for any m l x1 x2 x3 k exp_dummy99 exp_dummy00 exp_dummy01 exp_dummy02 exp_dummy03 exp_dummy04 exp_dummy05: qui gen OLSX=_b[X]
	qui gen OLSConst=_b[_c]	

	*Initial values for GMM
	for any m l x1 x2 x3 k exp_dummy99 exp_dummy00 exp_dummy01 exp_dummy02 exp_dummy03 exp_dummy04 exp_dummy05: qui gen initialX=OLSX
	qui gen initialConst=OLSConst

   set more off 
   *use  pfest.dta,clear
   capture drop firmid
   egen firmid =group(firm)
       sort firmid year
	   gen x1_lag=L.x1
	   gen x2_lag=L.x2
	   gen x3_lag=L.x3
   drop if phi==.
   drop if phi_lag==.
***更换gcic从1至29,分开每个行业来算,结果保存在gmm.xlsx中


   ****瑞丽加入了一行命令
dlw_translog
	*X=st_data(.,("const","m","l","x1","x2","x3","k","exp_dummy99","exp_dummy00","exp_dummy01","exp_dummy02","exp_dummy03","exp_dummy04","exp_dummy05"))
	*系数都保存在了beta_dlwtranslog中的，这个数据只有一行，第一列是const，第二列是m，其他依次，所以系数矩阵依次如下
	*tfp计算中，我看dlw没有用到const的系数，作为参考，只是说一下

**********
gen coef1=beta_dlwtranslog[1,2]
gen coef2=beta_dlwtranslog[1,3]
gen coef3=beta_dlwtranslog[1,4]
gen coef4=beta_dlwtranslog[1,5]
gen coef5=beta_dlwtranslog[1,6]
gen coef6=beta_dlwtranslog[1,7]
gen coef7=beta_dlwtranslog[1,1]
gen coef8=beta_dlwtranslog[1,8]
gen coef9=beta_dlwtranslog[1,9]
gen coef10=beta_dlwtranslog[1,10]
gen coef11=beta_dlwtranslog[1,11]
gen coef12=beta_dlwtranslog[1,12]
gen coef13=beta_dlwtranslog[1,13]
gen coef14=beta_dlwtranslog[1,14]

gen tfp=phi-coef7-coef1*m-coef2*l-coef3*x1-coef4*x2-coef5*x3-coef6*k-coef8*exp_dummy99-coef9*exp_dummy00-coef10*exp_dummy01-coef11*exp_dummy02-coef12*exp_dummy03-coef13*exp_dummy04-coef14*exp_dummy05
save "D:\Nanjing\2019\pollution2\for ACCRE\gmm`cc'",replace
restore
}

/*
use "D:\Nanjing\2019\pollution2\for ACCRE\gmm1", clear
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm2"

append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm3"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm4"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm5"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm6"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm7"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm8"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm9"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm10"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm11"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm12"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm13"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm14"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm15"
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm16.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm17.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm18.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm19.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm20.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm21.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm22.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm23.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm24.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm25.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm26.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm27.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm28.dta" 
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm29.dta" 


rename firm party_id
*keep year party_id tfp
*duplicates drop party_id year, force
save "D:\Nanjing\2019\pollution2\for ACCRE\gmm",replace
use "D:\Nanjing\2019\pollution2\for ACCRE\gmm",clear


 foreach cc of numlist 1/`r(max)'
 {append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm`cc'"
 save "D:\Nanjing\2019\pollution2\for ACCRE\gmm_all", replace
 }
 use "D:\Nanjing\2019\pollution2\for ACCRE\gmm_all",clear
 */
 
use "D:\Nanjing\2019\pollution2\for ACCRE\gmm1", clear
foreach cc of numlist 1/`rrm'{
append using "D:\Nanjing\2019\pollution2\for ACCRE\gmm`cc'"
 save "D:\Nanjing\2019\pollution2\for ACCRE\gmm_all", replace
 }
 use "D:\Nanjing\2019\pollution2\for ACCRE\gmm_all",clear
 rename firm party_id
*keep year party_id tfp q cic_adj
duplicates drop party_id year, force


sort party_id year
capture drop tfp_ini
gen tfp_ini=.
*****************************
*regenerate initial : first_d
*****************************

capture drop first_d
sort party_id year
by party_id: gen first_d=_n==1

replace tfp_ini=tfp_acf2 if first_d==1
capture drop tfp_new
bysort party_id: egen tfp_new=max(tfp_ini)
replace tfp_acf_new=tfp
replace treatment_tfp=cic_tariff_lag*tfp_new
replace treatment_so2=ventilation*tfp_new*so2_new2*dust_new2
replace treatment_dust=ventilation*tfp_new*so2_new2*dust_new2


******************************************结束*************************************************
